package com.pig4cloud.pig.base.config;
import com.alibaba.fastjson.JSONObject;
import com.pig4cloud.pig.base.api.vo.ConfigVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;
import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class BaseUtil {

	private static String HOST;
	private static String USERNAME;
	private static String PASSWORD;

	public static void setHOST(String HOST) {
		BaseUtil.HOST = HOST;
	}

	public static void setUSERNAME(String USERNAME) {
		BaseUtil.USERNAME = USERNAME;
	}

	public static void setPASSWORD(String PASSWORD) {
		BaseUtil.PASSWORD = PASSWORD;
	}

	private static String TOKEN_URL="/nacos/v1/auth/users/login";
    private static String SERVICE_URL="/nacos/v1/ns/catalog/services?";
    private static String CONFIG_URL="/nacos/v1/cs/configs?group=&appName=&config_tags=&tenant=&search=blur&username=nacos";
	private static String CONFIG_DETAIL="/nacos/v1/cs/configs?show=all&group=DEFAULT_GROUP&tenant=&namespaceId=";
    private static String SERVICE_DETAIL="/nacos/v1/ns/catalog/instances?clusterName=DEFAULT&groupName=DEFAULT_GROUP&pageSize=10&pageNo=1&namespaceId=";
    private static String CONFIG_UPDATE = "/nacos/v1/cs/configs?username=nacos";
	/***
	* @Description: 获取token
	* @Param: []
	* @return: java.lang.String
	* @Author: marco
	* @Date: 2024/5/30
	*/
	public static String  getAccessToken(){
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(HOST+TOKEN_URL);
        httpPost.addHeader("Content-Type","application/x-www-form-urlencoded");
		List<NameValuePair> params = new ArrayList<>();
		params.add(new BasicNameValuePair("username", USERNAME));
		params.add(new BasicNameValuePair("password", PASSWORD));
        String result = null;
        String access_token=null;
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(params,"utf-8"));
			HttpResponse response = httpClient.execute(httpPost);
			HttpEntity entity = response.getEntity();
			result = EntityUtils.toString(entity, "utf-8");
			System.out.println("result: " + result);
			JSONObject pa = JSONObject.parseObject(result);
			access_token = pa.getString("accessToken");
			System.out.println(access_token);
		} catch (IOException e) {
			e.printStackTrace();
		}
        return access_token;
    }
    public static String services(String name,String groupName,Integer pageNo,Integer pageSize){
		HttpClient httpClient = new DefaultHttpClient();
		String accessToken = getAccessToken();
		HttpGet httpGet = new HttpGet(HOST+SERVICE_URL+"accessToken="+accessToken+"&serviceNameParam="+name+"&groupNameParam="+groupName+"&pageNo="+pageNo+"&pageSize="+pageSize);
		String result = null;
		try {
			HttpResponse response = httpClient.execute(httpGet);
			HttpEntity entity = response.getEntity();
			result = EntityUtils.toString(entity, "utf-8");
			System.out.println("result: " + result);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
	public static String service(String serviceName){
		HttpClient httpClient = new DefaultHttpClient();
		String accessToken = getAccessToken();
		HttpGet httpGet = new HttpGet(HOST+SERVICE_DETAIL+"&accessToken="+accessToken+"&serviceName="+serviceName);
		String result = null;
		try {
			HttpResponse response = httpClient.execute(httpGet);
			HttpEntity entity = response.getEntity();
			result = EntityUtils.toString(entity, "utf-8");
			System.out.println("result: " + result);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
	public static String configs(String dataId,Integer pageNo,Integer pageSize){
		HttpClient httpClient = new DefaultHttpClient();
		String accessToken = getAccessToken();
		if(StringUtils.isBlank(dataId)){
			dataId="";
		}
		HttpGet httpGet = new HttpGet(HOST+CONFIG_URL+"&accessToken="+accessToken+"&dataId="+dataId+"&pageNo="+pageNo+"&pageSize="+pageSize);
		String result = null;
		try {
			HttpResponse response = httpClient.execute(httpGet);
			HttpEntity entity = response.getEntity();
			result = EntityUtils.toString(entity, "utf-8");
			System.out.println("result: " + result);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
	public static String config(String dataId){
		HttpClient httpClient = new DefaultHttpClient();
		String accessToken = getAccessToken();
		HttpGet httpGet = new HttpGet(HOST+CONFIG_DETAIL+"&accessToken="+accessToken+"&dataId="+dataId);
		String result = null;
		try {
			HttpResponse response = httpClient.execute(httpGet);
			HttpEntity entity = response.getEntity();
			result = EntityUtils.toString(entity, "utf-8");
			System.out.println("result: " + result);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String configUpdate (ConfigVO configVO){
		HttpClient httpClient = new DefaultHttpClient();
		String accessToken = getAccessToken();
		if (StringUtils.isBlank(configVO.getDataId())){
			configVO.setDataId("");
		}
		if (StringUtils.isBlank(configVO.getGroup())){
			configVO.setGroup("");
		}
		if (StringUtils.isBlank(configVO.getContent())){
			configVO.setContent("");
		}

		HttpPost httpPost = new HttpPost(HOST + CONFIG_UPDATE + "&accessToken=" + accessToken);
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
		builder.setCharset(Charset.forName("utf-8"));
		ContentType contentType = ContentType.create("multipart/form-data", Charset.forName("UTF-8"));
		builder.addTextBody("dataId", configVO.getDataId(), contentType);
		builder.addTextBody("group", configVO.getGroup(), contentType);
		builder.addTextBody("content", configVO.getContent(), contentType);
		HttpEntity entity = builder.build();
		String result = null;
		try {
			httpPost.setEntity(entity);
			HttpResponse response = httpClient.execute(httpPost);
			HttpEntity entity1 = response.getEntity();
			result = EntityUtils.toString(entity1, "utf-8");
			System.out.println("result: " + result);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}

}
